package com.yidu.best.basicSetup.fundset.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yidu.best.basicSetup.accountset.dao.AccountDao;
import com.yidu.best.basicSetup.accountset.domain.Account;
import com.yidu.best.basicSetup.custodian.dao.CustodianDao;
import com.yidu.best.basicSetup.custodian.domain.Custodian;
import com.yidu.best.basicSetup.fundset.dao.FundDao;
import com.yidu.best.basicSetup.fundset.domain.Fund;
import com.yidu.best.basicSetup.fundset.domain.FundVo;
import com.yidu.best.basicSetup.fundset.service.FundService;
import com.yidu.best.basicSetup.trusteeset.dao.TrusteeDao;
import com.yidu.best.basicSetup.trusteeset.domain.Trustee;
import com.yidu.best.bond.cashInventory.dao.CashInventoryDao;
import com.yidu.best.bond.cashInventory.domain.CashInventory;
import com.yidu.best.bond.securitiesInventory.dao.SecuritiesInventoryDao;
import com.yidu.best.bond.securitiesInventory.domain.SecuritiesInventory;
import com.yidu.best.util.IDUtil;

@Service
public class FundServiceImpl implements FundService {
	@Autowired
	private FundDao fundDao;
	@Autowired
	private AccountDao accountDao;
	@Autowired
	private CashInventoryDao cashInventoryDao;
<<<<<<< HEAD
=======
	@Autowired
	private TrusteeDao trusteeDao;
	@Autowired
	private CustodianDao custodianDao;
>>>>>>> branch 'master' of https://gitee.com/luzhenhe/BestFund.git
	
	public FundDao getFundDao() {
		return fundDao;
	}

	public void setFundDao(FundDao fundDao) {
		this.fundDao = fundDao;
	}

	/**
	 * 分页查询
	 */
	@Override
	public Map<String, Object> findQueryPageFund(FundVo fundVo) {
		int page = (fundVo.getPage() - 1) * fundVo.getLimit();
		fundVo.setPage(page);
		int count = fundDao.findCountFund(fundVo);
		List<Fund> fundList = fundDao.findQueryPageFund(fundVo);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("code", 0);
		map.put("msg", "");
		map.put("count", count);
		map.put("data", fundList);
		return map;
	}

	/**
	 * 删除
	 */
	@Override
	public int deleteFund(String fundId) {
		return this.fundDao.deleteFund(fundId);
	}

	/**
	 * 修改
	 */
	@Override
	public int updateFund(Fund fund) {
		return this.fundDao.updateFund(fund);
	}

	/**
	 * 添加
	 */
	@Override
	public int addFund(Fund fund) {
		// 随机一个16位数的Id
		fund.setFundId(IDUtil.getUUID());
		fund.setCreateTime(new Date());
		fund.setRegisterDate(new Date());
<<<<<<< HEAD
		int rs = fundDao.addFund(fund);
		Account account = accountDao.findAccountById(fund.getAccountId());
		if (rs == 1) {

			rs = initCashInvetory(fund, account);
		}

=======
		
		int rs = fundDao.addFund(fund);
		//调用Dao层方法
		initCashInvetory(fund);
		
>>>>>>> branch 'master' of https://gitee.com/luzhenhe/BestFund.git
		return rs;
	}

	/**
	 * 查询托管人Id和名字
	 */
<<<<<<< HEAD
	@Override
	public List<Fund> findByAllFund() {

		return fundDao.findByAllFund();
=======
	public List<Trustee> findByAllTrustee() {
		
		return trusteeDao.findByAllTrustee();
	}
	
	/**
	 * 查询管理人ID和名字
	 * @return
	 */
	public List<Custodian> findByAllCustodian(){
		
		return custodianDao.findByAllCustodian();
	}
	
	/**
	 * 查询现金
	 * @param fund
	 * @return
	 */
	public List<Account> findByAllAccount(){
		
		return accountDao.findByAllAccount();
>>>>>>> branch 'master' of https://gitee.com/luzhenhe/BestFund.git
	}
<<<<<<< HEAD

	private int initCashInvetory(Fund fund, Account account) {
		//初始化现在库存
		CashInventory cashInventory = new CashInventory(
				IDUtil.getUUID(), 
				IDUtil.getNo("XJKC"), 
				fund.getFundId(),
				fund.getFundNo(), 
				fund.getFundName(), 
				account.getAccountId(), 
				account.getAccountNO(),
				account.getAccountName(), 
				BigDecimal.valueOf(0), 
				new Date(), 
=======
	
	
	private int initCashInvetory(Fund fund) {
		System.out.println(fund);
		Account account = accountDao.findAccountById(fund.getAccountId());
		CashInventory cashInventory = new CashInventory(
				IDUtil.getUUID(),
				IDUtil.getNo("XJKC"),
				fund.getFundId(),
				fund.getFundNo(),
				fund.getFundName(),
				account.getAccountId(),
				account.getAccountNO(),
				account.getAccountName(),
				BigDecimal.valueOf(0),
				new Date(),
>>>>>>> branch 'master' of https://gitee.com/luzhenhe/BestFund.git
				null);
		return cashInventoryDao.insertCachInventory(cashInventory);
	}

	

	

}
